/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[遇见知启蒙，邂逅框架梦]
 * 
 * https://zhiqim.org/project/zhiqim_framework/zhiqim_httpd.htm
 *
 * Zhiqim Httpd is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package org.zhiqim.httpd.test.manage;

import java.util.List;

import org.zhiqim.httpd.HttpSession;
import org.zhiqim.httpd.HttpSessionUser;
import org.zhiqim.httpd.websocket.HttpWebSocket;
import org.zhiqim.httpd.websocket.HttpWebSocketServicer;
import org.zhiqim.kernel.Z;
import org.zhiqim.kernel.annotation.AnAlias;
import org.zhiqim.kernel.logging.Log;
import org.zhiqim.kernel.logging.LogFactory;
import org.zhiqim.kernel.model.seqs.Sequence;
import org.zhiqim.kernel.schedule.Scheduler;
import org.zhiqim.kernel.schedule.Task;
import org.zhiqim.kernel.util.DateTimes;
import org.zhiqim.kernel.util.Ids;

/**
 * WebSocket监听服务
 *
 * @version v7.0.1 @author zouzhigang 2021-7-4 新建与整理
 */
@AnAlias("WebSocketServicer")
public class WebSocketServicer extends  HttpWebSocketServicer implements Task
{
    private static final Log log = LogFactory.getLog(WebSocketServicer.class);
    private Sequence sequence = new Sequence(8);
    
    public WebSocketServicer()
    {//注意要预留firstMillis一点时间，否则manager还未设置
        Z.glb().getc(Scheduler.class).addIntervalTask(this, 20, 1000 * 5);
    }
    
    @Override
    public void execute()
    {
        List<HttpWebSocket> list = manager.get("WebSocketServicer");
        for (HttpWebSocket conn : list)
        {
            conn.sendText(DateTimes.getDateTimeSSSString() + ": " + sequence.nextString());
        }
    }
    
    @Override
    public void onOpen(HttpWebSocket conn)
    {
        conn.getWebSocketManager().getContext();
        HttpSessionUser sessionUser = conn.getSession().getSessionUser();
        if (sessionUser == null)
        {
            log.info("open:sessionUser unlogin");
        }
        else
        {
            log.info("open:"+sessionUser.getSessionName());
        }
    }

    @Override
    public void onClose(HttpWebSocket conn, int code)
    {
        HttpSessionUser sessionUser = conn.getSession().getSessionUser();
        if (sessionUser == null)
            return;
        
        log.info("close:"+conn.getSession().getSessionUser().getSessionName());
    }

    @Override
    public void onException(HttpWebSocket conn, Throwable e)
    {
        HttpSessionUser sessionUser = conn.getSession().getSessionUser();
        if (sessionUser == null)
            return;
        
        log.info("exception:"+sessionUser.getSessionName());
    }

    @Override
    public void onText(HttpWebSocket conn, String message)
    {
        HttpSession session = conn.getSession();
        
        HttpSessionUser sessionUser = session.getSessionUser();
        if (sessionUser == null)
        {
            log.info("sessionUser:null,message:"+message);
            return;
        }
        
        log.info("message:"+sessionUser.getSessionName());
    }

    @Override
    public void onBinary(HttpWebSocket conn, byte[] message)
    {
        HttpSessionUser sessionUser = conn.getSession().getSessionUser();
        if (sessionUser == null)
            return;
        
        log.info("message:"+sessionUser.getSessionName());
        conn.sendText(""+Ids.longId());
    }
}
